package powell.nflpredictionproject.display;



import java.awt.Color;
import powell.nflpredictionproject.display.MainPanel;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashSet;
import javax.swing.*;
import powell.nflpredictionproject.database.InformationFromDB;

/**
 *
 * @author Verti_000
 */
public class UserPanel extends JPanel
{

    private JLabel awayTeamDesc = new JLabel("AWAY");
    private JLabel homeTeamDesc = new JLabel("HOME");
    private JButton calculate = new JButton("Calculate");
    private String[] teamNames =
    {
        "Arizona Cardinals",
        "Atlanta Falcons",
        "Baltimore Ravens",
        "Buffalo Bills",
        "Carolina Panthers",
        "Chicago Bears",
        "Cincinnati Bengals",
        "Cleveland Browns",
        "Dallas Cowboys",
        "Denver Broncos",
        "Detroit Lions",
        "Green Bay Packers",
        "Houston Texans",
        "Indianapolis Colts",
        "Jacksonville Jaguars",
        "Kansas City Chiefs",
        "Miami Dolphins",
        "Minnesota Vikings",
        "New England Patriots",
        "New Orleans Saints",
        "New York Giants",
        "New York Jets",
        "Oakland Raiders",
        "Philadelphia Eagles",
        "Pittsburgh Steelers",
        "Saint Louis Rams",
        "San Diego Chargers",
        "San Francisco 49ers",
        "Seattle Seahawks",
        "Tampa Bay Buccaneers",
        "Tennessee Titans",
        "Washington Redskins"
    };
    private JComboBox awayTeam;
    private JComboBox homeTeam;

    public UserPanel()
    {
        PopulatingComboBoxes();

        this.setBackground(Color.BLACK);
        
        this.setForeground(Color.WHITE);
        
        this.setLayout(new GridLayout(2, 2));

        this.add(awayTeamDesc);
        this.add(awayTeam);
        this.add(homeTeamDesc);
        this.add(homeTeam);
        
        homeTeam.setSelectedIndex(1);
        
        calculate.addActionListener(new ActionListenerCalculate());

        this.setVisible(true);
    }

    public void PopulatingComboBoxes()
    {
        awayTeam = new JComboBox(teamNames);
        homeTeam = new JComboBox(teamNames);
        
        awayTeam.setBackground(Color.WHITE);
        homeTeam.setBackground(Color.WHITE);
        
        awayTeam.setForeground(Color.BLACK);
        homeTeam.setForeground(Color.BLACK);
        
        awayTeamDesc.setForeground(Color.WHITE);
        homeTeamDesc.setForeground(Color.WHITE);

        awayTeam.addActionListener(new TeamActionListener());
        homeTeam.addActionListener(new TeamActionListener());
    }

    public JPanel ButtonPanel()
    {
        JPanel buttonPanel = new JPanel();

        buttonPanel.add(getCalculate());
        
        calculate.setBackground(Color.WHITE);
        
        calculate.setForeground(Color.BLACK);
        
        buttonPanel.setBackground(Color.BLACK);

        return buttonPanel;
    }
    
    public String getAwayTeam()
    {
        return awayTeam.getSelectedItem().toString();
    }
    
    public String getHomeTeam()
    {
        return homeTeam.getSelectedItem().toString();
    }

    /**
     * @return the calculate
     */
    public JButton getCalculate()
    {
        return calculate;
    }

    private class TeamActionListener implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            if (awayTeam.getSelectedIndex() == homeTeam.getSelectedIndex())
            {
                JOptionPane.showMessageDialog(null, "Please select another team because this team is already selected in the away box", "ERROR!", 1);
                if (homeTeam.getSelectedIndex() != 0)
                {
                    homeTeam.setSelectedIndex(awayTeam.getSelectedIndex() - 1);
                }
                
                if (homeTeam.getSelectedIndex() == 0)
                {
                    homeTeam.setSelectedIndex(awayTeam.getSelectedIndex() + 1);
                }
            }
        }
    }

    private class ActionListenerCalculate implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent e)
        {
            if (homeTeam.isEnabled() == true)
            {
            InformationFromDB victor = new InformationFromDB();
            
            //victor.GetStatsPanel(awayTeam.getSelectedItem().toString(), homeTeam.getSelectedItem().toString()); 
            victor.calculateVictor(awayTeam.getSelectedItem().toString(), homeTeam.getSelectedItem().toString());
            }
        }
    }
}
